package com.rickpan.dto.share;

import com.rickpan.entity.FileShare;
import java.time.LocalDateTime;

/**
 * 分享响应DTO
 * 
 * @author RickPan Team
 * @since 2025-01-05
 */
public class ShareResponse {

    /**
     * 分享ID
     */
    private Long id;

    /**
     * 分享码
     */
    private String shareCode;

    /**
     * 分享链接
     */
    private String shareUrl;

    /**
     * 文件ID
     */
    private Long fileId;

    /**
     * 文件名
     */
    private String fileName;

    /**
     * 原始文件名
     */
    private String originalName;

    /**
     * 文件大小
     */
    private Long fileSize;

    /**
     * 分享者ID
     */
    private Long userId;

    /**
     * 分享者用户名
     */
    private String userName;

    /**
     * 分享类型
     */
    private FileShare.ShareType shareType;

    /**
     * 访问密码（仅在用户查看自己的分享时返回）
     */
    private String password;

    /**
     * 允许下载
     */
    private Boolean allowDownload;

    /**
     * 允许预览
     */
    private Boolean allowPreview;

    /**
     * 过期时间
     */
    private LocalDateTime expireTime;

    /**
     * 访问次数
     */
    private Integer accessCount;

    /**
     * 最大访问次数
     */
    private Integer maxAccessCount;

    /**
     * 分享状态
     */
    private FileShare.ShareStatus status;

    /**
     * 创建时间
     */
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    private LocalDateTime updatedAt;

    /**
     * 是否已过期
     */
    private Boolean expired;

    /**
     * 是否可用
     */
    private Boolean available;

    /**
     * 分享备注/说明
     */
    private String description;

    // 构造函数
    public ShareResponse() {}

    // Getter 和 Setter 方法
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getShareCode() { return shareCode; }
    public void setShareCode(String shareCode) { this.shareCode = shareCode; }

    public String getShareUrl() { return shareUrl; }
    public void setShareUrl(String shareUrl) { this.shareUrl = shareUrl; }

    public Long getFileId() { return fileId; }
    public void setFileId(Long fileId) { this.fileId = fileId; }

    public String getFileName() { return fileName; }
    public void setFileName(String fileName) { this.fileName = fileName; }

    public String getOriginalName() { return originalName; }
    public void setOriginalName(String originalName) { this.originalName = originalName; }

    public Long getFileSize() { return fileSize; }
    public void setFileSize(Long fileSize) { this.fileSize = fileSize; }

    public Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }

    public String getUserName() { return userName; }
    public void setUserName(String userName) { this.userName = userName; }

    public FileShare.ShareType getShareType() { return shareType; }
    public void setShareType(FileShare.ShareType shareType) { this.shareType = shareType; }

    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    public Boolean getAllowDownload() { return allowDownload; }
    public void setAllowDownload(Boolean allowDownload) { this.allowDownload = allowDownload; }

    public Boolean getAllowPreview() { return allowPreview; }
    public void setAllowPreview(Boolean allowPreview) { this.allowPreview = allowPreview; }

    public LocalDateTime getExpireTime() { return expireTime; }
    public void setExpireTime(LocalDateTime expireTime) { this.expireTime = expireTime; }

    public Integer getAccessCount() { return accessCount; }
    public void setAccessCount(Integer accessCount) { this.accessCount = accessCount; }

    public Integer getMaxAccessCount() { return maxAccessCount; }
    public void setMaxAccessCount(Integer maxAccessCount) { this.maxAccessCount = maxAccessCount; }

    public FileShare.ShareStatus getStatus() { return status; }
    public void setStatus(FileShare.ShareStatus status) { this.status = status; }

    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }

    public LocalDateTime getUpdatedAt() { return updatedAt; }
    public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }

    public Boolean getExpired() { return expired; }
    public void setExpired(Boolean expired) { this.expired = expired; }

    public Boolean getAvailable() { return available; }
    public void setAvailable(Boolean available) { this.available = available; }

    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }

    /**
     * 从FileShare实体创建响应DTO
     */
    public static ShareResponse fromEntity(FileShare fileShare, String fileName, String userName, String baseUrl) {
        ShareResponse response = new ShareResponse();
        response.setId(fileShare.getId());
        response.setShareCode(fileShare.getShareCode());
        response.setShareUrl(baseUrl + "/s/" + fileShare.getShareCode());
        response.setFileId(fileShare.getFileId());
        response.setFileName(fileName);
        response.setOriginalName(fileName); // 设置原始文件名
        response.setUserId(fileShare.getUserId());
        response.setUserName(userName);
        response.setShareType(fileShare.getShareType());
        // 对于加密密码，返回特殊标识而不是实际加密字符串
        String password = fileShare.getPassword();
        if (password != null && password.startsWith("$2")) {
            // BCrypt加密密码，返回标识表示已设置密码
            response.setPassword("***ENCRYPTED***");
        } else {
            // 明文密码或空密码，直接返回
            response.setPassword(password);
        }
        response.setAllowDownload(fileShare.getAllowDownload());
        response.setAllowPreview(fileShare.getAllowPreview());
        response.setExpireTime(fileShare.getExpireTime());
        response.setAccessCount(fileShare.getAccessCount());
        response.setMaxAccessCount(fileShare.getMaxAccessCount());
        response.setStatus(fileShare.getStatus());
        response.setCreatedAt(fileShare.getCreatedAt());
        response.setUpdatedAt(fileShare.getUpdatedAt());
        response.setExpired(fileShare.isExpired());
        response.setAvailable(fileShare.isAvailable());
        response.setDescription(fileShare.getDescription());
        return response;
    }

    /**
     * 简化版本，用于列表展示
     */
    public static ShareResponse fromEntitySimple(FileShare fileShare, String fileName, String baseUrl) {
        ShareResponse response = new ShareResponse();
        response.setId(fileShare.getId());
        response.setShareCode(fileShare.getShareCode());
        response.setShareUrl(baseUrl + "/s/" + fileShare.getShareCode());
        response.setFileId(fileShare.getFileId());
        response.setFileName(fileName);
        response.setOriginalName(fileName); // 设置原始文件名
        response.setShareType(fileShare.getShareType());
        response.setAccessCount(fileShare.getAccessCount());
        response.setStatus(fileShare.getStatus());
        response.setCreatedAt(fileShare.getCreatedAt());
        response.setExpired(fileShare.isExpired());
        response.setAvailable(fileShare.isAvailable());
        return response;
    }
}
